<?php
namespace Tongcheng\Controller;
use Think\Controller;
header('content-type:application:json;charset=utf8');
class CreditController extends CommonController {
    public function index(){
        $this->display();
    }


    //积分签到设置
    public function sign_set()
    {
        $re = M('credit_sign_set')->where(['wid'=>session('wid')])->find();
        if(IS_POST){
            $data = [];
            $sign_con_day = json_decode(json_encode(I('sign_con_day')),true);
            $sign_con_credit = json_decode(json_encode(I('sign_con_credit')),true);
            foreach($sign_con_credit as $k=> $v){
                if($k ===0){
                    $data['sign_con_credit'] = $v;
                }else{
                    if($v){
                        $data['sign_con_credit'] .=','. $v;
                    }
                }

            }
            foreach($sign_con_day as $k=> $v){
                if($k ===0){
                    $data['sign_con_day'] = $v;
                }else{
                    if($v){
                        $data['sign_con_day'] .=','. $v;
                    }

                }

            }
            $data['first'] = I('first');
            $data['normal'] = I('normal');

            if($re){
                $r =M('credit_sign_set')->where(['id'=>$re['id']])->save($data);
            }else{
                $data['wid'] = session('wid');
                $r =M('credit_sign_set')->add($data);
            }
            if($r){
                $this->success('修改成功');
            }else{
                $this->error('网络忙，请稍后再试！');
            }
        }else{
            $arr =[];
            $re['sign_con_day'] = explode(',',$re['sign_con_day']);
            $re['sign_con_credit'] = explode(',',$re['sign_con_credit']);
            if(is_array($re['sign_con_day']))
            {
                foreach($re['sign_con_day'] as $k=>$v){
                    $arr[$k] = ['sign_con_day'=>$v,'sign_con_credit'=>$re['sign_con_credit'][$k]];
                }
            }
            $re['arr'] = $arr;
            $this->assign('info',$re);
            $this->display();
        }
    }
    /*
     * 基本设置
     *
     * */
    public function setBasic () {
        if ($_POST) {
            $post = $_POST;
            $post['wid'] = session('wid');
            $is_have = M('credit_setint')->where(['wid' => session('wid')])->find();
            if ($is_have) {
                $row = M('credit_setint')->where(['wid' => session('wid')])->save($post);
            } else {
                $row = M('credit_setint')->add($post);
            }
            if ($row) {
                $this->ajaxReturn(['error' => 0,'info' => '提交成功']);
            } else {
                $this->ajaxReturn(['error' => 1,'info' => '网络忙']);
            }
        }
        $info = M('credit_setint')->where(['wid' => session('wid')])->find();
        $this->assign('info', $info);
        $this->display();
    }

    /*
     * 商品管理
     *
     * */
    public function goodsAdmin () {
        $where['wid'] = session('wid');
        if ($_POST['is_stop'] != null) {
            $where['is_stop'] = $_POST['is_stop'];
        }
        if ($_POST['type_id'] != null) {
            $where['type_id'] = $_POST['type_id'];
        }
        if ($_POST['name'] != null) {
            $where['name'] = ['like', '%'.$_POST['name'].'%'];
        }
        $list = M('credit_goods')->where($where)->order('order_num desc')->select();
        $news_cate = M('credit_type')->where(['wid' => session('wid')])->select();
        $this->assign('type', $news_cate);
        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 添加商品
     *
     * */
    public function add_goods () {
        $m = M('credit_goods');
        if (IS_AJAX) {
            $post = $_POST;
            unset($post['file']);
            $post['wid'] = session('wid');
            $post['time'] = date('Y-m-d H:i:s', time());
            if ($post['id']) {
                $row = $m->where(['id' => $post['id']])->save($post);
            } else {
                $row = $m->add($post);
            }
            $row ? $this->ajaxReturn(['status' => 1, 'info' => '提交成功!']) : $this->ajaxReturn(['status' => 0, 'info' => '网络忙']);
        }
        if ($_GET['id']) {
            $vo = $m->where(['id' => $_GET['id']])->find();
            $this->assign('vo', $vo);
        }
        $news_cate = M('credit_type')->where(['wid' => session('wid')])->select();
        $this->assign('news_cate', $news_cate);
        $this->display();
    }

    /*
     * 商品分类管理
     *
     * */
    public function goodsCate () {
        $list = M('credit_type')->where(['wid' => session('wid')])->order('sort desc')->select();
        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 添加分类
     *
     * */
    public function addCate () {
        $m = M('credit_type');
        $post = $_POST;
        $data['wid'] = session('wid');
        $data['sort'] = $post['sort'];
        $data['name'] = $post['name'];
        $data['img'] = $post['img'];
        if (IS_AJAX) {
            if ($post['id']) {
                $row = $m->where(['id' => $post['id']])->save($data);
            } else {
                $row = $m->add($data);
            }
            $row ? $this->ajaxReturn(['status' => 1, 'info' => '提交成功!']) : $this->ajaxReturn(['status' => 0, 'info' => '网络忙']);
        }
        if ($_GET['id']) {
            $vo = $m->where(['id' => $_GET['id']])->find();
            $this->assign('vo', $vo);
        }
        $this->display();
    }

    /*
     * 删除分类
     *
     * */
    public function delCate () {
        $model = M("credit_type");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];
        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }

    /*
     * 删除商品
     *
     * */
    public function delGoods () {
        $model = M("credit_goods");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];
        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
    
    /*
     * 轮播
     * 
     * */
    public function flash () {
        $vo = M('credit_flash')->alias('a')
                                ->field('a.*, b.name as goods_name')
                                ->join('tp_credit_goods b ON a.goods_id = b.id')
                                ->where(['a.wid' => session('wid')])
                                ->order('a.order_num desc')
                                ->select();
        $this->assign('list', $vo);
        $this->display();
    }

    /*
     * 添加轮播
     *
     * */
    public function addFlash () {
        $m = M('credit_flash');
        $post = $_POST;
        $data['img'] = $post['img'];
        $data['order_num'] = $post['order_num'];
        $data['goods_id'] = $post['goods_id'];
        $data['time'] = date('Y-m-d H:i:s', time());
        $data['wid'] = session('wid');
        $data['url'] = '/pages/creditshop/commodity_details?id='.$post['goods_id'];

        if (IS_AJAX) {
            if ($post['id']) {
                $row = $m->where(['id' => $post['id']])->save($data);
            } else {
                $row = $m->add($data);
            }
            $row ? $this->ajaxReturn(['status' => 1, 'info' => '提交成功!']) : $this->ajaxReturn(['status' => 0, 'info' => '网络忙']);
        }
        if ($_GET['id']) {
            $vo = $m->where(['id' => $_GET['id']])->find();
            $this->assign('vo', $vo);
        }

        //商品
        $goods = M('credit_goods')->where(['wid' => session('wid')])->select();
        $this->assign('goods', $goods);
        $this->display();
    }

    /*
     * 删除商品
     *
     * */
    public function delFlash () {
        $model = M("credit_flash");
        $map['wid'] = session('wid');
        $map['id'] = $_POST['id'];
        if($model->where($map)->delete()){
            $this->success("删除成功！");
        }else{
            $this->error("网络忙，请稍后再试！");
        }
    }
    
    /*
     * 待审核
     * 
     * */
    public function examinePro () {
        //
        $list = M('credit_order')->alias('a')
                                ->field('a.*, b.img as goodimg, b.name as goodname')
                                ->join('tp_credit_goods b ON a.goodsid = b.id')
                                ->where(['a.wid' => session('wid'), 'a.status' => 1])
                                ->order('a.time desc')
                                ->select();
        $this->assign('list', $list);
        $this->display();
    }

    /*
     * 已审核
     *
     * */
    public function examineProd () {
        //
        $list = M('credit_order')->alias('a')
            ->field('a.*, b.img as goodimg, b.name as goodname')
            ->join('tp_credit_goods b ON a.goodsid = b.id')
            ->where(['a.wid' => session('wid'), 'a.status' => 2])
            ->order('a.time desc')
            ->select();
        $this->assign('list', $list);

        $this->display();
    }

    /*
     * 已拒绝
     *
     * */
    public function examineProNo () {
        //
        $list = M('credit_order')->alias('a')
            ->field('a.*, b.img as goodimg, b.name as goodname')
            ->join('tp_credit_goods b ON a.goodsid = b.id')
            ->where(['a.wid' => session('wid'), 'a.status' => 3])
            ->order('a.time desc')
            ->select();
        $this->assign('list', $list);

        $this->display();
    }

    /*
     * 通过审核
     *
     * */
    public function adopt () {
        $id = $_POST['id'];
        $row = M('credit_order')->where(['id' => $id])->save(['status' => 2]);
        $row ? $this->success('已通过') : $this->error('网络忙');
    }

    /*
     * 拒绝兑换
     *
     * */
    public function refuse () {
        $id = $_POST['id'];
        $row = M('credit_order')->where(['id' => $id])->save(['status' => 3]);
        $row ? $this->success('已拒绝') : $this->error('网络忙');
    }
}